#!/bin/sh

# $Id: streaming,v 1.6 2009-07-24 08:39:27 brian Exp $
# Copyright (c)2007 Brian Manning <brian at portaboom dot com>

# PLEASE DO NOT E-MAIL THE AUTHOR ABOUT THIS SOFTWARE
# The proper venue for questions is the LACK mailing list at:
# http://groups.google.com/linuxack or <linuxack.googlegroups.com>

# picks up things like colorize()
if ! [ -e $LACK_FUNCTIONS ]; then
    LACK_FUNCTIONS="/etc/scripts/lack_functions.sh"
fi # if ! [ -e $LACK_FUNCTIONS ]
source $LACK_FUNCTIONS

ACTION=$1
BINARY="/etc/scripts/streaming.sh"
[ -x "$BINARY" ] || exit 1
BASENAME="streaming"
DESC="MP3 streaming"

# go into /proc/cmdline and see what options were used
# stream URL
file_parse "/proc/cmdline" "nostream"
NO_STREAMING=$PARSED
if [ "x${NO_STREAMING}" != "x" ]; then
    # we don't want streaming; bail
    exit 0
fi # if [ "x${NO_STREAMING}" != "x" ]

# stream URL
file_parse "/proc/cmdline" "url"
STREAM_URL=$PARSED
# some defaults
if [ "x$STREAM_URL" == "x" ]; then
    STREAM_URL="http://stream.portaboom.com:7767/vault"
fi # if [ "x$STREAM_URL" == "x" ]

# are we using wget for streaming?
file_parse "/proc/cmdline" "wget"
USE_WGET=$PARSED
if [ "x$USE_WGET" != "x" ]; then
    USE_WGET=1
fi # if [ "x$USE_WGET" == "x" ]

# export these here so they get propagated to other scripts
export STREAM_URL USE_WGET

# act on the argument passed in to this script
case "$ACTION" in
  vars)
    # echo out what commandline variables are parsed by this script
    echo "${BASENAME}:"
    exit 0
  ;;
  start)
    colorize $S_TIP "${BASENAME}: Starting ${DESC}"
    # are we using wget for streaming?
    if [ "x$USE_WGET" == "x" ]; then
        # nope
        $BINARY --url $STREAM_URL &
        check_exit_status $? "${BASENAME}: mpg123 ${STREAM_URL}"
    else
        # yep
        $BINARY --url $STREAM_URL --use-wget &
        check_exit_status $? "${BASENAME}: wget/mpg123 ${STREAM_URL}"
    fi # if [ "x$USE_WGET" != "x" ]
    ;;
  stop)
    colorize $S_TIP "${BASENAME}: Stopping ${DESC}"
    $BINARY --killall
    check_exit_status $? "${BASENAME}: mpg123 ${STREAM_URL}"
    ;;
  restart|force-reload)
     $0 stop
     $0 start
    ;;
  *)
    echo "Usage: $BASENAME {start|stop|restart|force-reload}" >&2
    exit 3
    ;;
esac

exit 0
# vi: set shiftwidth=4 tabstop=4 filetype=sh :
# конец!
